/* 
-serves as a mediator for the view that is binded to the domain model
-this is where processing of view related gestures occur like logic for disabling a button
-logic for displaying components in the view 
*/
package poler.model.presentation
{
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.controls.DateField;
	import mx.controls.Tree;
	import mx.core.UIComponent;
	
	import poler.classes.AbstractPM;
	import poler.control.events.View1Event;
	import poler.model.ModelLocator;
	import poler.model.domain.domainView1;
	import poler.util.CustomValidator;
	import poler.view.components.MyImageHoler;
	import poler.view.view1;
	
	[Bindable]
	public class View1PM extends AbstractPM
	{ 
		public var _BOview1:domainView1;
		//
		public var acDp:ArrayCollection = new ArrayCollection();
		public var isTop:Boolean = false;	
		private var cv:CustomValidator = new CustomValidator();
			
		public function View1PM()
		{
			_BOview1 = ModelLocator.getInstance().dmView1;
		}
			
		public function init():void{
			for(var i:int = 0; i < 5; i++){
				var st:String = 'Other '+i;
				acDp.addItem({label:st,
				data:st
				});
			}
		}
		public function clearForm(e:Event,obj:Object=null):void{
			cv._toggleEnableValidators();
			if(obj != null){
				obj.fname.text = '';
				obj.lname.text = '';
				obj.pnumber.text = '';
				obj.ns.value = 0;
				obj.cbDept.text = '';
				obj.bdate.selectedDate = null;
			}
			isTop = false;
		}
		public function select():void{
			new View1Event('select').dispatch();
			isTopHandler();
		}
		public function isTopHandler():void{
			if(_BOview1.acDetails.length > 0){
				var tobj:Object = _BOview1.acDetails.getItemAt(_BOview1.acDetails.length-1); 
				isTop = (tobj != null);
			}else{
				isTop = true;
			}
		}
		public function itemChandler(e:Event, obj:Object,ui:UIComponent):void{
			view1(ui).myIHolder.deActivateCam();
			
			trace('tree: '+Tree(e.target).selectedItem);
			var st:Array = String(obj.Name).split(' ');			
			view1(ui).formModel.Id = obj.Id;
			view1(ui).bdate.selectedDate = DateField.stringToDate(obj.Birthdate,"MM/DD/YYYY");
			view1(ui).fname.text = st[0].toString();
			view1(ui).lname.text = st[1].toString();
			view1(ui).pnumber.text = obj.Phone;
			view1(ui).ns.value =  Number(obj.Title)	;
			view1(ui).cbDept.text = obj.Department.toString();
			//
			ModelLocator.getInstance().ContactId = obj.Id;
			loadImage();
			trace('ModelLocator.getInstance().ContactId = obj.Id: '+ModelLocator.getInstance().ContactId);
		}
		public function loadImage():void{
			if(ModelLocator.getInstance().ContactId.length > 0){
				new View1Event('selectImage').dispatch();
			}
		}
		public function validate(cont:Object,vmodel:Object = null,img:MyImageHoler = null):void{
			cv = new CustomValidator();
				cv._vTextField(cont.fname,'-First Name');
				cv._vTextField(cont.lname,'-Last Name');
				cv._vTextField(cont.pnumber,'-Phone Number');
				cv._vNumericStepper(cont.ns,'-Title');
				cv._vTextField(cont.cbDept,'-Department');
				cv._vDate(cont.bdate,'-Birth Date');
				
				if(ModelLocator.getInstance().ContactId != null)
					cv._vTree(cont.dgView1,'-No Selected Item');
					
				if(cv.showWarnings()){
					ModelLocator.getInstance().dmView1.arBase64String = img.arBase64String;
					beginSave(vmodel);
				}else{
					//throw new Error('Errors Found!');
				}
				
			}
		
		private function beginSave(obj:Object):void{
			new View1Event('saveData',obj).dispatch();
		}
		public function deleterec(obj:Object):void{
			if(obj != null){
				new View1Event('deleterec',obj).dispatch();
			}
		}
		override protected function handleFirstShow():void
		{			
			
		}		
		
	}
}